

function projectEstimationPanel(estimationFormPanelId){
	
	 estimationPanelId = {
			xtype		: 'fieldset',
			title		: 'Estimation Details',
			id			: 'estimationPanelId',
			width		: '98%',
			labelWidth	: 120,
			autoHeight	: true,
			items		: [
			     		    {
			     		    	    xtype      : 'hidden',
			     		    	    id         : 'estimationId',
			     		    	    fieldLabel : 'Estimation Id'
			     		    },
							{
									xtype		: 'compositefield',
									fieldLabel	: 'Estimated Start Date',
									items		: [
									     		    {
									     		    	xtype		: 'datefield',
									     		   	 	id			: 'estStartDate',
									     		   	 	width		: 130,
									     		   	 	format		: 'Y-m-d'
									     		    },
									     		    {xtype : 'displayfield', value: 'Estimated End Date: ', width : 120},
									     		    {
									     		    	xtype		: 'datefield',
									     		    	id			: 'estEndDate',
									     		   	 	width		: 130,
									     		   	 	format		: 'Y-m-d'	
									     		    }
									     		   ]
							},
							{
								xtype		: 'compositefield',
								fieldLabel	: 'Actual Start Date',
								items		: [
								     		    {
								     		    	xtype		: 'textfield',
								     		   	 	id			: 'actStartDate',
								     		   	 	width		: 130,
								     		   	 	value		: '0',
								     		   	 	disabled	: true
								     		    },
								     		    {xtype : 'displayfield', value: 'Actual End Date: ', width : 120},
								     		    {
								     		    	xtype		: 'textfield',
								     		    	id			: 'actEndDate',
								     		   	 	width		: 130,
								     		   	 	value		: '0',
								     		   	 	disabled	: true
								     		    }
								     		   ]
							},
							{
								xtype		: 'compositefield',
								fieldLabel	: 'Estimated Budget',
								items		: [
								     		    {
								     		    	xtype		: 'textfield',
								     		   	 	id			: 'estBudget',
								     		   	 	width		: 130	
								     		    },
								     		    {xtype : 'displayfield', value: 'Estimated Hrs.: ', width : 120},
								     		    {
								     		    	xtype		: 'textfield',
								     		    	id			: 'estHrs',
								     		   	 	width		: 130	
								     		    }
								     		   ]
							},
							{
								xtype		: 'compositefield',
								fieldLabel	: 'Actual Budget',
								items		: [
								     		    {
								     		    	xtype		: 'textfield',
								     		   	 	id			: 'actBudget',
								     		   	 	width		: 130,
								     		   	 	value		: '0',
								     		   	 	disabled	: true
								     		    },
								     		    {xtype : 'displayfield', value: 'Actual Hrs.: ', width : 120},
								     		    {
								     		    	xtype		: 'textfield',
								     		    	id			: 'actHrs',
								     		   	 	width		: 130,
								     		   	 	value		: '0',
								     		   	 	disabled	: true
								     		    }
								     		   ]
							},
							{
								xtype		: 'compositefield',
								fieldLabel	: 'Budget Differences',
								items		: [
								     		    {
								     		    	xtype		: 'textfield',
								     		   	 	id			: 'budgetDiff',
								     		   	 	width		: 130,
								     		   	 	value		: '0',
								     		   	 	disabled	: true
								     		    },
								     		    {xtype : 'displayfield', value: 'Hrs. Differences: ', width : 120},
								     		    {
								     		    	xtype		: 'textfield',
								     		    	id			: 'hrsDiff',
								     		   	 	width		: 130,
								     		   	 	value		: '0',
								     		   	 	disabled	: true
								     		    }
								     		   ]
							}
			     		   ],
			    bbar		: 	{
				 				 xtype		: 'toolbar',
				 				 buttonAlign: 'right',
				 				 items		: [
				 				      		   	{
				 				      		   		xtype	: 'button',
				 				      		   		id		: 'saveEstButton',
				 				      		   		text	: 'Save',
				 				      		   		handler :estimateValidateAndSave
				 				      		   		/*handler : function(){
				 				      		   		         	saveEstimationPanel(estimationFormPanelId);
				 				      		   	              }*/
				 				      		   	},
				 				      		   	'-',
				 				      		   	{
				 				      		   		xtype	: 'button',
				 				      		   		id		: 'updateEstButton',
				 				      		   		text	: 'Update'
				 				      		   	},'-',
				 				      		   	{
				 				      		   		xtype   : 'button',
				 				      		   		id      : 'cancleButton',
				 				      		   		text    : 'Cancel',
				 				      		   		handler : estimationCloseWin
				 				      		   	}
				 				      		   ]
			 				  }    			
	};
	 
	 var estimationFormPanel = new Ext.FormPanel({
			id			: estimationFormPanelId,
			autoHeight	: true,
			frame       : true,
			width		: '99%',
			items		: [estimationPanelId]
		});
		
	return estimationFormPanel;
}



// Function Start 

//Date Validation
	Ext.apply(Ext.form.VTypes, {
	daterange : function(val, field) {
		var date = field.parseDate(val);
	
		if(!date){
			return;
		}
		if (field.startDateField && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) {
			var start = Ext.getCmp(field.startDateField);
			start.setMaxValue(date);
			start.validate();
			this.dateRangeMax = date;
		} 
		else if (field.endDateField && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) {
			var end = Ext.getCmp(field.endDateField);
			end.setMinValue(date);
			end.validate();
			this.dateRangeMin = date;
		}
		return true;
	}
	});

// function for project basic info validation 

function projectInfoValidateAndSave(form,formIds)
{
	 var validationSuccess = simpleFormValidation(form, formIds);
	 if(validationSuccess == 'true'){
		 		saveProject(form);	
			}
};
	
 // function for project estimation validation
 function estimateValidateAndSave(){
	 var estStartDateValue      = Ext.getCmp("estStartDate").getValue();
	  var estEndDateValue       = Ext.getCmp("estEndDate").getValue();
	  var estBudgetValue        = Ext.getCmp("estBudget").getValue();
	  var estHoursValue         = Ext.getCmp("estHrs").getValue();
	  var msg="";
	  if(estStartDateValue=='')
      {
  		 msg=" --> Please Enter Est Start Date. <br>";
	  }
	  if(estEndDateValue=='')
      {
  		 msg=msg+" --> Please Enetr Est End Date. <br>";
	  }
	  if(estBudgetValue =='')
      {
         msg =msg+" --> Please Enter Estimate Budget. <br>";
      }
	  if(estHoursValue=='')
      {
         msg =msg+" --> Please Enter estimate Hours. <br>";
      }
	  if(msg == ''){
		  saveEstimationPanel(estimationFormPanelId);	
 	  	}
 	  	else{
 		  var msgBox = Ext.Msg.show({
 			 msg	: '<font color="red"><b>'+msg,
 			 title	: 'Validation Message',
 			 buttons: Ext.Msg.OK,
 			 autWidth: true,
 			 frame	: true,
 			 animEl	: 'elId'
 		  });
 	  }
	  
 };


function reloadContactTo(selectedLeadId){
	var contactCombo = Ext.getCmp('contactTo');
	contactCombo.store.removeAll();
	contactCombo.store.reload({
						params : {leadId : selectedLeadId}
						});
};


function saveProject(formPanel){
		var action = Ext.getCmp('saveButton').getText();
	    var message = 'No action taken.';
	     if(action == 'Save'){
	  	   message = 'Saved Successfully!';
	     }
	     if(action == 'Update'){
	  	   message = 'Updated Successfully!';
	     }
	     
		var values = formPanel.getForm().getValues();
		var data = Ext.encode(values);
		Ext.Ajax.request({
			url		: 'project/save?action='+action,
			method  : 'POST',
			success :function(result, request){
	  			    var jsonData = Ext.util.JSON.decode(result.responseText); 
		  			var responsedProjectId = jsonData.data;
		   			alert("Project"+message);
		   			disableProjectForm();
		   			Ext.getCmp('projectId').setValue(responsedProjectId);
		   			
		   		  },
		    failure : function(){
		            	  alert("failure");
		            },
		              jsonData : data
		});
};


function saveEstimationPanel(formPanelId){
	
	var formPanel =  Ext.getCmp(formPanelId);
	//formPanel.el.mask('Please wait', 'x-mask-loading');
	var values = formPanel.getForm().getValues();
	var data = Ext.encode(values);
	Ext.Ajax.request({
		url		: 'project/saveEstimationInfo',
		method  : 'POST',
		success : function()
		          {
		              alert("Estimation saved successfully.");
		              //formPanel.el.unmask();
	              },
	    failure : function(){
	            	  alert("failure");
	            	  //formPanel.el.unmask();
	              },
	              jsonData : data
	});
};
	
	var win;
	function closeSearchWindow(){
		win.close();
	};

function showCustomerSearchWindow(){
	var customerStore   = CRM.commons.createStore('customerStoreid',CRM.SPComp.customerDataArray,'project/showCustomer');
	var customerSearchCriteriaStore = CRM.commons.createComboStore('customerSearchCriteriaStoreid', 'project/customerSearchCriteria');
	
	var searchCustomerPanel = CRM.SPComp.createSearchComp('spSearchCustomerGridPanel', 'searchCriteria', 'criteriaValue', customerStore, CRM.SPComp.createCustomerColumnModel(), customerSearchCriteriaStore);
	var searchGrid = new Ext.getCmp('spSearchCustomerGridPanel');
	searchGrid.getColumnModel().setHidden(12,true);
	win = new Ext.Window({
			title		: 'Dynamic Search Window',
			id			: 'searchWindow',
			width		: 1100,
			height		: 450,
			border		: true,
			plain		: false,
			closable	: true,
			collapsible : true,
			animCollapse: true,
			draggable 	: true,
			maximizable : false,
			onEsc		: true,
			model		: true,
			items		: [
			     		   searchCustomerPanel
			     		  ],
			bbar        : {
			                    xtype       : 'toolbar',
			                    buttonAlign : 'right',
			                    items       : [
			                        {
			                            xtype   : 'button',
			                            text    : 'OK',
			                            handler : function(){
			                        	   var record = Ext.getCmp('spSearchCustomerGridPanel').getSelectionModel().getSelected();
			                        	   Ext.getCmp('leadId').setValue(record.get('id'));
		                        		   Ext.getCmp('companyCustomer').setValue(record.get('companyName'));
		                        		   reloadContactTo(record.get('id'));
		                                   closeSearchWindow();
		                                   //reloadContactTo();
			                               }
			                        }
			                    ]
			                }     		  
		
	});
		
win.show(this);
return win;

};

function showDetails(selectedProjectId){
	
	 var formPanel = Ext.getCmp('projectPanel');
	 formPanel.getForm().load({
	 method  : 'GET',
     url     : 'project/showProjectDetails?projectId='+selectedProjectId,
		success : function() {
		        },
        failure:function() { 
         		  alert ("Failure");
         	}
	});
	Ext.getCmp('projectMainTab').setActiveTab('detailsTab');
};


	projectIdsArray = ["projectName","companyCustomer","contactTo","contactToId","status","statusId","priority","priorityId","actualStartDate","actualEndDate","actualBudget","actualHrs","projectDetails"];

function disableProjectForm(){
	//var projectForm = Ext.getCmp('projectPanel'); 
    //doFormReadOnly(projectForm, projectIdsArray, true);
	Ext.getCmp('projectName').disable();
	Ext.getCmp('companyCustomer').disable();
	Ext.getCmp('contactTo').disable();
	Ext.getCmp('status').disable();
	Ext.getCmp('priority').disable();
	Ext.getCmp('actualStartDate').disable();
	Ext.getCmp('actualEndDate').disable();
	Ext.getCmp('actualBudget').disable();
	Ext.getCmp('actualHrs').disable();
	Ext.getCmp('projectDetails').disable();
	
	Ext.getCmp('saveButton').disable();
	Ext.getCmp('editButton').enable();
	
	Ext.getCmp('taskPanel').enable();
	Ext.getCmp('membersPanel').enable();
	Ext.getCmp('issuePanel').enable();
	Ext.getCmp('engagementPanel').enable();
	Ext.getCmp('documentsPanel').enable();
	
	
	
};

function editProjectForm(){
	//var projectForm = Ext.getCmp('projectPanel'); 
   // doFormReadOnly(projectForm, projectIdsArray, false);


	Ext.getCmp('projectName').enable();
	Ext.getCmp('companyCustomer').enable();
	Ext.getCmp('contactTo').enable();
	Ext.getCmp('status').enable();
	Ext.getCmp('priority').enable();
	Ext.getCmp('actualStartDate').enable();
	Ext.getCmp('actualEndDate').enable();
	Ext.getCmp('actualBudget').enable();
	Ext.getCmp('actualHrs').enable();
	Ext.getCmp('projectDetails').enable();
	
	Ext.getCmp('saveButton').enable();
	Ext.getCmp('saveButton').setText('Update');	
	
	Ext.getCmp('taskPanel').enable();
	Ext.getCmp('membersPanel').enable();
	Ext.getCmp('issuePanel').enable();
	Ext.getCmp('engagementPanel').enable();
	Ext.getCmp('documentsPanel').enable();
};

function newProjectForm(){
	editProjectForm();
	//var projectForm = Ext.getCmp('projectPanel'); 
	//projectForm.getForm().reset();
	Ext.getCmp('projectName').setValue(null);
	Ext.getCmp('companyCustomer').setValue(null);
	Ext.getCmp('contactTo').setValue(null);
	Ext.getCmp('status').setValue(null);
	Ext.getCmp('priority').setValue(null);
	Ext.getCmp('actualStartDate').setValue(null);
	Ext.getCmp('actualEndDate').setValue(null);
	Ext.getCmp('actualBudget').setValue(null);
	Ext.getCmp('actualHrs').setValue(null);
	Ext.getCmp('projectDetails').setValue(null);

	Ext.getCmp('saveButton').enable();
	Ext.getCmp('editButton').disable();
	Ext.getCmp('saveButton').setText('Save');
	
	Ext.getCmp('taskPanel').disable();
	Ext.getCmp('membersPanel').disable();
	Ext.getCmp('issuePanel').disable();
	Ext.getCmp('engagementPanel').disable();
	Ext.getCmp('documentsPanel').disable();
};


